import { defineComponent, reactive, onBeforeUnmount, provide, inject } from 'vue';
const APP_NAVIGATOR_PROVIDER = "@@app-navigator";
function getRoute() {
    let locationHash = window.location.hash || '';
    console.log({ locationHash });
    if (locationHash.charAt(0) === '#') {
        locationHash = locationHash.slice(1);
    }
    console.log({ locationHash });
    const [path, hash] = (decodeURIComponent(locationHash)).split('#');
    console.log({ path }, { hash });
    return {
        path,
        hash,
    };
}
function useAppNavigator(props) {
    const currentRoute = getRoute();
    !currentRoute.path && (currentRoute.path = props.defaultPath);
    const state = reactive({
        route: currentRoute
    });
    const methods = {
        go(path) {
            window.location.hash = encodeURIComponent(path);
        }
    };
    const handler = {
        hashchange: () => {
            state.route = getRoute();
        }
    };
    const refer = {
        state,
        methods,
    };
    window.addEventListener('hashchange', handler.hashchange);
    onBeforeUnmount(() => window.removeEventListener('hashchange', handler.hashchange));
    provide(APP_NAVIGATOR_PROVIDER, refer);
    return refer;
}
export function injectAppNavigator() {
    return inject(APP_NAVIGATOR_PROVIDER);
}
export const AppNavigator = defineComponent({
    name: 'app-navigator',
    props: {
        defaultPath: String,
    },
    setup(props, setupContext) {
        useAppNavigator(props);
        return () => !!setupContext.slots.default ? setupContext.slots.default() : null;
    }
});
//# sourceMappingURL=app-navigator.jsx.map